package com.ruoyi.combat.service.useralignment.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.combat.domain.useralignment.CombatBlogZh;
import com.ruoyi.combat.domain.useralignment.CombatRelationZh;
import com.ruoyi.combat.mapper.useralignment.CombatBlogZhMapper;
import com.ruoyi.combat.mapper.useralignment.CombatRelationZhMapper;
import com.ruoyi.combat.util.zhihu.ZhihuApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.combat.mapper.useralignment.CombatUserZhMapper;
import com.ruoyi.combat.domain.useralignment.CombatUserZh;
import com.ruoyi.combat.service.useralignment.ICombatUserZhService;

/**
 * 知乎用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-10
 */
@Service
public class CombatUserZhServiceImpl implements ICombatUserZhService {
    @Autowired
    private CombatUserZhMapper combatUserZhMapper;
    @Autowired
    private CombatRelationZhMapper  combatRelationZhMapper;
    @Autowired
    private CombatBlogZhMapper combatBlogZhMapper;

    @Autowired
    private ZhihuApiUtil zhihuApiUtil;

    /**
     * 查询知乎用户信息
     *
     * @param id 知乎用户信息主键
     * @return 知乎用户信息
     */
    @Override
    public CombatUserZh selectCombatUserZhById(Long id) {
        return combatUserZhMapper.selectCombatUserZhById(id);
    }

    /**
     * 查询知乎用户信息列表
     *
     * @param combatUserZh 知乎用户信息
     * @return 知乎用户信息
     */
    @Override
    public List<CombatUserZh> selectCombatUserZhList(CombatUserZh combatUserZh) {
        return combatUserZhMapper.selectCombatUserZhList(combatUserZh);
    }

    /**
     * 新增知乎用户信息
     *
     * @param combatUserZh 知乎用户信息
     * @return 结果
     */
    @Override
    public int insertCombatUserZh(CombatUserZh combatUserZh) {
        return combatUserZhMapper.insertCombatUserZh(combatUserZh);
    }

    /**
     * 修改知乎用户信息
     *
     * @param combatUserZh 知乎用户信息
     * @return 结果
     */
    @Override
    public int updateCombatUserZh(CombatUserZh combatUserZh) {
        return combatUserZhMapper.updateCombatUserZh(combatUserZh);
    }

    /**
     * 批量删除知乎用户信息
     *
     * @param ids 需要删除的知乎用户信息主键
     * @return 结果
     */
    @Override
    public int deleteCombatUserZhByIds(Long[] ids) {

        for (Long id : ids){
            CombatUserZh combatUserZh = combatUserZhMapper.selectCombatUserZhById(id);
            String userId = combatUserZh.getUserId();
            List<CombatRelationZh> zhList = combatRelationZhMapper.selectRelationByUserId(userId);
            for (CombatRelationZh zh : zhList){
                combatRelationZhMapper.deleteCombatRelationZhById(zh.getId());
            }
            CombatBlogZh combatBlogZh = new CombatBlogZh();
            combatBlogZh.setUserId(userId);
            List<CombatBlogZh> combatBlogZhs = combatBlogZhMapper.selectCombatBlogZhList(combatBlogZh);
            for (CombatBlogZh blogZh : combatBlogZhs){
                combatBlogZhMapper.deleteCombatBlogZhById(blogZh.getId());
            }
        }


        return combatUserZhMapper.deleteCombatUserZhByIds(ids);
    }

    @Override
    public int crawlZhByUserId(Long id) {
        List<String> list = new ArrayList<>();
        CombatUserZh combatUserZh = combatUserZhMapper.selectCombatUserZhById(id);
        if (combatUserZh != null) {
            list.add(combatUserZh.getUserId());
        }

        if (list.size() > 0) {
            zhihuApiUtil.collectData(list);
        }
        return list.size();
    }

    @Override
    public int crawlZhByUserIds(Long[] ids) {
        List<String> list = new ArrayList<>();
        for (Long id : ids) {
            CombatUserZh combatUserZh = combatUserZhMapper.selectCombatUserZhById(id);
            if (combatUserZh != null) {
                list.add(combatUserZh.getUserId());
            }
        }

        if (list.size() > 0) {
            zhihuApiUtil.collectData(list);
        }

        return list.size();
    }

    @Override
    public int crawlAllZhByUserIds() {
        List<String> list = new ArrayList<>();
        List<CombatUserZh> combatUserZhs = combatUserZhMapper.selectCombatUserZhList(new CombatUserZh());
        for (CombatUserZh zh : combatUserZhs) {
            list.add(zh.getUserId());
        }

        if (list.size() > 0) {
            zhihuApiUtil.collectData(list);
        }
        return list.size();
    }

    /**
     * 删除知乎用户信息信息
     *
     * @param id 知乎用户信息主键
     * @return 结果
     */
    @Override
    public int deleteCombatUserZhById(Long id) {
        return combatUserZhMapper.deleteCombatUserZhById(id);
    }
}
